import 'package:flutter/material.dart';
import 'package:flutter_application_demo/container.dart';
import 'package:flutter_application_demo/home.dart';
import 'package:flutter_application_demo/layout.dart';
import 'package:flutter_application_demo/base_component.dart';
import 'package:flutter_application_demo/scroll.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        'new_route': (context) => const NewRoute(),
        'route_test': (context) => const RouterTestRoute(),
        'layout': (context) => const LayoutMain(),
        'button_widget': (context) => const ButtonWidget(),
        'img_widget': (context) => const ImageWidget(),
        'switch_widget': (context) => const SwitchAndCheckBoxTestRoute(),
        'textfield_widget': (context) => const TextFieldWidget(),
        'form_widget': (context) => const FormTestRoute(),
        'flex_widget': (context) => const FlexLayoutTestRoute(),
        'layout_widget': (context) => const LayoutListRoute(),
        'wrap_widget': (context) => const WrapRoute(),
        'stack_widget': (context) => const StackRoute(),
        'after_layout_widget': (context) => const AfterLayoutRoute(),
        'padding_widget': (context) => const PaddingRoute(),
        'scaffold_widget': (context) => const ScaffoldRoute(),
        'scroll_list_widget': (context) => const ScrollListRoute(),
        'list_widget': (context) => const ListViewRoute(),
        'list_build_widget': (context) => const ListBuildRoute(),
        'list_separated_widget': (context) => const ListSeparatedRoute(),
        'list_head_widget': (context) => const ListHeadRoute(),
        'page_widget': (context) => const PageViewRoute(),
        'tab_widget': (context) => const TabViewRoute(),
        'future_widget': (context) => const FutureBuilderRoute(),
      },
      home: const MyHomePage(title: 'hello, world'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  // Obtain shared preferences.

  // final SharedPreferences prefs = await SharedPreferences.getInstance();

  @override
  void initState() {
    super.initState();
    print('initState');
    // _getCounts();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print("didChangeDependencies");
  }

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Future<void> _getCounts() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    int? counterint = prefs.getInt("counter");
    if (counterint != null) {
      _counter = counterint;
    }
  }

  Future<void> _saveCounts() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt('counter', _counter);
    print('保存成功');
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // Text(
            //   '你可以点击多次：',
            //   style: Theme.of(context).textTheme.displayMedium,
            // ),
            // Text(
            //   '下面是显示次数：',
            //   style: Theme.of(context).textTheme.displaySmall,
            // ),
            // Text(
            //   '$_counter',
            //   style: Theme.of(context).textTheme.displayMedium,
            // ),
            // TextButton(
            //   onPressed: () {
            //     Navigator.push(
            //       context,
            //       MaterialPageRoute(builder: (context) {
            //         return const NewRoute();
            //       }),
            //     );
            //   },
            //   child: const Text('push_next_page'),
            // ),

            Text(
              '$_counter',
              style: Theme.of(context).textTheme.displayMedium,
            ),
            // TextButton(
            //   onPressed: () => {_saveCounts()},
            //   child: const Text(
            //     '保存点击次数',
            //     style: TextStyle(fontSize: 20),
            //   ),
            // ),

            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'layout')},
              child: const Text('跳转文本布局'),
            ),
            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'button_widget')},
              child: const Text('跳转按钮布局'),
            ),
            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'img_widget')},
              child: const Text('跳转图片布局'),
            ),
            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'switch_widget')},
              child: const Text('跳转单选复选框'),
            ),
            TextButton(
              onPressed: () =>
                  {Navigator.pushNamed(context, 'textfield_widget')},
              child: const Text('跳转输入框'),
            ),
            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'form_widget')},
              child: const Text('跳转表单'),
            ),

            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'layout_widget')},
              child: const Text('跳转布局'),
            ),

            TextButton(
              onPressed: () =>
                  {Navigator.pushNamed(context, 'scroll_list_widget')},
              child: const Text('跳转滚动组件'),
            ),

            TextButton(
              onPressed: () => {Navigator.pushNamed(context, 'future_widget')},
              child: const Text('发起请求'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

//********************************************************************** */
//示例代码
// TapboxA 管理自身状态.
class TapboxA extends StatefulWidget {
  const TapboxA({Key? key}) : super(key: key);
  @override
  _TapboxAState createState() => _TapboxAState();
}

class _TapboxAState extends State<TapboxA> {
  bool _active = false;
  void _handleTap() {
    setState(() {
      _active = !_active;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _handleTap,
      child: Container(
        child: Center(
          child: Text(
            _active ? 'Active' : 'Inactive',
            style: const TextStyle(fontSize: 32.0, color: Colors.cyan),
          ),
        ),
        width: 200.0,
        height: 200.0,
        decoration:
            BoxDecoration(color: _active ? Colors.lightGreen : Colors.grey),
      ),
    );
  }
}

//********************************************************************** */
///父Widget管理子Widget的状态
class ParentWidget extends StatefulWidget {
  const ParentWidget({Key? key}) : super(key: key);

  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {
  bool _activeState = false;
  void _handleTapBoxChange(bool newValue) {
    setState(() {
      _activeState = newValue;
    });
  }

  @override
  Widget build(BuildContext context) {
    return TapboxB(
      onChanged: _handleTapBoxChange,
      active: _activeState,
    );
  }
}

class TapboxB extends StatelessWidget {
  const TapboxB({Key? key, this.active = false, required this.onChanged})
      : super(key: key);
  final bool active;
  final ValueChanged<bool> onChanged;

  void _handleTap() {
    onChanged(!active);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _handleTap,
      child: Container(
        child: Center(
          child: Text(
            active ? 'Active' : 'Inactive',
            style: const TextStyle(fontSize: 32.0, color: Colors.white),
          ),
        ),
        width: 200.0,
        height: 200.0,
        decoration: BoxDecoration(
          color: active ? Colors.lightGreen[700] : Colors.grey[600],
        ),
      ),
    );
  }
}


//********************************************************************** */
